package com.cn.hadoop.hbase;

import com.alibaba.fastjson.JSON;
import com.cn.util.RandomName;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class HbaseTest {
    Connection connection;
    Admin admin;
    Table hTable;
    Configuration configuration;

    @Before
    public void first() throws IOException {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node3,node2,node4");
        connection = ConnectionFactory.createConnection(configuration);
    }

    @Test
    public void create() throws IOException {
        admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("users");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf");
        hTableDescriptor.addFamily(hColumnDescriptor);
        admin.createTable(hTableDescriptor);
    }

    @Test
    public void insert() throws IOException {
        hTable = connection.getTable(TableName.valueOf("users"));
        Put put = new Put("1111111111".getBytes());
        put.addColumn("cf".getBytes(), "name".getBytes(), "张三".getBytes());
        put.addColumn("cf".getBytes(), "age".getBytes(), "22".getBytes());
        put.addColumn("cf".getBytes(), "sex".getBytes(), "boy".getBytes());
        hTable.put(put);
    }

    @Test
    public void insert1() throws IOException {
        hTable = connection.getTable(TableName.valueOf("users"));
//        double random = (Math.random() * 100) +
        for (Integer j = 0; j < 1000; j++) {
            Put put = new Put(j.toString().getBytes());
            String sex = (int) (Math.random() * 2) == 1 ? "boy" : "girl";
            put.addColumn("cf".getBytes(), "name".getBytes(), RandomName.getName(sex).getBytes());
            String age = Integer.toString((int) (Math.random() * 50) + 1);
            put.addColumn("cf".getBytes(), "age".getBytes(), age.getBytes());
            put.addColumn("cf".getBytes(), "sex".getBytes(), (sex).getBytes());
            hTable.put(put);
        }
    }

    @Test
    public void insertJson() throws IOException {
        hTable = connection.getTable(TableName.valueOf("users"));
        for (Integer j = 0; j < 1000; j++) {
            Put put = new Put(j.toString().getBytes());
            String sex = (int) (Math.random() * 2) == 1 ? "boy" : "girl";
            User user = new User();
            user.setAge(Integer.toString((int) (Math.random() * 50) + 1));
            user.setName(RandomName.getName(sex));
            user.setSex(sex);
//            Cell cell = CellUtil.createCell("cf".getBytes(), JSON.toJSONBytes(user));
//            put.add(cell);
            put.add("cf".getBytes(), "users".getBytes(), JSON.toJSONBytes(user));
            hTable.put(put);
        }
    }

    @Test
    public void get() throws IOException {
        hTable = connection.getTable(TableName.valueOf("users"));
        Get get = new Get("1111111111".getBytes());
        System.out.println(hTable.get(get));
        HTableDescriptor tableDescriptor = hTable.getTableDescriptor();
        System.out.println(tableDescriptor);
//        get.addColumn("cf".getBytes(), "name".getBytes());
        Result result = hTable.get(get);
        System.out.println(result);
        Cell name = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
        Cell age = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
        Cell sex = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
        System.out.println(sex.getValue());
        System.out.println(new String(result.getValue("cf".getBytes(), "name".getBytes())));
        System.out.println(new String(CellUtil.cloneValue(name).toString()));
        System.out.println(CellUtil.cloneValue(age).toString());
        System.out.println(CellUtil.cloneRow(sex).toString());
    }


    @Test
    public void scan() throws IOException {
        hTable = connection.getTable(TableName.valueOf("users"));
        Scan scan = new Scan();
        scan.setStartRow("999".getBytes());
//        scan.setStopRow("2".getBytes());
        ResultScanner scanner = hTable.getScanner(scan);
        scanner.forEach(e -> {
            System.out.println("输出" + new String(e.getRow()));
            for (KeyValue v : e.raw()) {
                System.out.println(new String(v.getKey()) + "----------" + new String(v.getValue()));
            }
        });
    }

    @Test
    public void delete() throws IOException {
        //删除指定的数据，根据rowkey
        hTable = connection.getTable(TableName.valueOf("users"));
        Delete delete = new Delete("999".getBytes());
        hTable.delete(delete);
        //删除表
//        Admin admin = connection.getAdmin();
//        admin.disableTable(TableName.valueOf("test"));
//        admin.deleteTable(TableName.valueOf("test"));
    }

    @After
    public void close() throws IOException {
//        admin.close();
        connection.close();
    }
}
